import React from 'react';
import { render } from 'react-dom';
import { Router, hashHistory } from 'react-router';
import { Provider } from 'react-redux';
import request from 'axios';

import configureStore from '../common/configure-store.js';
import routes from '../common/routes/root.jsx';

const initialState = window.__INIT_REDUX_STATE__;
const store = configureStore(initialState);

const rootContainerEl = document.getElementById('root-container');

// 配置 axios 默认 transformResponse
request.defaults.transformResponse = [(data, headers) => {
    let response = data;

    if (headers['content-type'] && /application\/json/.test(headers['content-type'])) {
        response = JSON.parse(data);
    }

    return response;
}];

// 防止页面后退
history.pushState(null, null, document.URL);
window.addEventListener('popstate', function () {
    history.pushState(null, null, document.URL);
});

render(
    <Provider store={store}>
        <Router children={routes} history={hashHistory} />
    </Provider>,
    rootContainerEl
);